package com.hp.erp.controller;

import com.hp.erp.entity.Functions;
import com.hp.erp.entity.JsonResult;
import com.hp.erp.service.IFunctionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FunctionController {

    @Autowired
    private IFunctionsService functionsService;

    @RequestMapping("/functions/username")
    public JsonResult getFunctionsByUsername(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof User && principal != null) {
            User user = (User) principal;
            //按用户名查询菜单权限
            List<Functions> functions = functionsService.getFunctionsByUsername(user.getUsername());
            return new JsonResult(1, functions);
        }
        return new JsonResult(0, null);
    }

    @RequestMapping("/functions")
    public JsonResult getAllFunctions(){
        return new JsonResult(1, functionsService.getAllFunctions());
    }

    @RequestMapping("/functions/role/{roleId}")
    public JsonResult getFunctionsIdsByRoleId(@PathVariable String roleId){
        return new JsonResult(1,functionsService.getFunctionsIdsByRoleId(roleId));
    }
}
